﻿#include "mainwindow.h"

#include <QDebug>
#include <QKeyEvent>
#include <QMediaPlayer>
#include <QGraphicsView>

#include "Help/aboutdialog.h"
#include "Help/instrucdialog.h"
#include "MVC/graphicsscene.h"
#include "Settings/scoreboarddialog.h"
#include "Settings/settingdialog.h"
#include "ui_mainwindow.h"

#if (QT_VERSION >= QT_VERSION_CHECK(6,0,0))
#include <QAudioOutput>
#endif


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    ui->mainToolBar->hide();

    view = new QGraphicsView(this);
    scene = new GraphicsScene();
    view->setScene(scene);
    view->hide();

    connect(scene, &GraphicsScene::showRank, this, [this](int mode) {
        ScoreboardDialog dlg(this);
        dlg.showMode(mode);
        dlg.exec();
    });
    connect(scene, &GraphicsScene::gameOver, this, [this] {
        ui->action_settings->setEnabled(true);
    });

    this->setCentralWidget(view);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_action_quit_triggered()
{
    this->close();
}

void MainWindow::on_action_start_triggered()
{
    view->show();
    view->setFocus();
    scene->game_start();
    ui->action_pause->setEnabled(true);
    ui->action_pause->setChecked(false);
    ui->action_settings->setEnabled(false);
}

void MainWindow::closeEvent(QCloseEvent *event)
{
    view->close();
    QMainWindow::closeEvent(event);
}

/**
 * @brief   重新实现了快捷键
 * @details Alt+Enter -- 全屏/取消全屏
 *          ESC -- 取消全屏
 *          F5 -- 重新开始
 *          F6 -- 暂停/取消暂停
 */
void MainWindow::keyPressEvent(QKeyEvent *event)
{
    switch (event->key()) {
    case Qt::Key_Escape:
        ui->menuBar->show();
        ui->statusBar->show();
        this->showNormal();
        break;
    case Qt::Key_Enter:
    case Qt::Key_Return:
        if (event->modifiers() == Qt::AltModifier) {
            if (this->isFullScreen()) {
                ui->menuBar->show();
                ui->statusBar->show();
                this->showNormal();
            } else {
                ui->menuBar->hide();
                ui->statusBar->hide();
                this->showFullScreen();
            }
        }
        break;
    case Qt::Key_F5:
        on_action_start_triggered();
        break;
    case Qt::Key_F6:
        ui->action_pause->setChecked(!ui->action_pause->isChecked());
        break;
    default:
        break;
    }

    QMainWindow::keyPressEvent(event);
}

/**
 * @brief   切换全屏
 */
void MainWindow::on_action_fullScreen_triggered()
{
    ui->menuBar->hide();
    ui->statusBar->hide();
    this->showFullScreen();
}

/**
 * @brief   操作说明
 */
void MainWindow::on_action_instructions_triggered()
{
    InstrucDialog dlg(this);
    dlg.exec();
}

/**
 * @brief   关于
 */
void MainWindow::on_action_about_triggered()
{
    AboutDialog dlg(this);
    dlg.exec();
}

/**
 * @brief   暂停
 */
void MainWindow::on_action_pause_toggled(bool arg1)
{
    // view->game_pause(arg1);
    scene->game_pause(arg1);
    ui->action_settings->setEnabled(arg1);
}

/**
 * @brief   设置
 */
void MainWindow::on_action_settings_triggered()
{
    SettingDialog dlg(this);
    if (dlg.exec() == QDialog::Accepted) {
        scene->set_player(ST->settings()->value("PlayerName", QDir::home().dirName()).toString());
        scene->statistics_reset();

#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
        ST->backgroundplayer()->audioOutput()->setVolume(ST->settings()->value("MusicLevel", 3).toInt() * 10);
#else
        ST->backgroundplayer()->setVolume(ST->settings()->value("MusicLevel", 3).toInt() * 10);
#endif

        ST->soundList()->setVolume(ST->settings()->value("SoundEffectLevel", 3).toInt() / 10.0);
    }
}

/**
 * @brief   得分榜
 */
void MainWindow::on_action_scoreboard_triggered()
{
    ScoreboardDialog dlg(this);
    dlg.exec();
}

